<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: qscreenahigl_qws.cpp Example File (qtopiacore/ahigl/qscreenahigl_qws.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">qscreenahigl_qws.cpp Example File<br /><sup><sup>qtopiacore/ahigl/qscreenahigl_qws.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &quot;qscreenahigl_qws.h&quot;
 #include &quot;qwindowsurface_ahigl_p.h&quot;

 #include &lt;QWSServer&gt;
 #include &lt;QMap&gt;
 #include &lt;QTimer&gt;
 #include &lt;QTimeLine&gt;

 #include &lt;qwindowsystem_qws.h&gt;
 #include &lt;private/qwindowsurface_qws_p.h&gt;
 #include &lt;private/qtextengine_p.h&gt;

 #include &lt;GLES/egl.h&gt;
 #include &lt;GLES/gl.h&gt;
 #include &lt;math.h&gt;

 const int animationLength = 1500;
 const int frameSpan = 20;

 static GLuint createTexture(const QImage &amp;img);

 class QAhiGLCursor : public QScreenCursor
 {
 public:
     QAhiGLCursor() : texture(0) {}
     ~QAhiGLCursor();

     void set(const QImage &amp;image, int hotx, int hoty);

     GLuint texture;
 };

 QAhiGLCursor::~QAhiGLCursor()
 {
     if (texture)
         glDeleteTextures(1, &amp;texture);
 }

 void QAhiGLCursor::set(const QImage &amp;image, int hotx, int hoty)
 {
     if (texture)
         glDeleteTextures(1, &amp;texture);

     if (image.isNull())
         texture = 0;
     else
         texture = createTexture(image.convertToFormat(QImage::Format_ARGB32));

     QScreenCursor::set(image, hotx, hoty);
 }

<span class="comment"> /*!
   \class QAhiGLScreenPrivate
   The QAhiGLScreenPrivate class contains state information for class QAhiGLScreen.

   An instance of this class points to the owning instance of
   class QAhiGLScreen. This class uses a QTimer to limit the
   update frequency.
  */</span>
 class QAhiGLScreenPrivate : public QObject
 {
     Q_OBJECT

 public:
     QAhiGLScreenPrivate(QAhiGLScreen *s);

 public slots:
     void windowEvent(QWSWindow *w, QWSServer::WindowEvent e);
     void redrawScreen();

 public:
     QAhiGLScreen *screen;
     QAhiGLCursor *cursor;

     EGLContext eglContext;
     EGLDisplay eglDisplay;
     EGLSurface eglSurface;

     QTimer updateTimer;
     bool doEffects;
 };

 class ShowAnimation : public QTimeLine
 {
 public:
     ShowAnimation(QAhiGLScreenPrivate *screen);
     qreal valueForTime(int msec);
 };

 struct WindowInfo
 {
     WindowInfo() : texture(0), animation(0) {}

     GLuint texture;
     QPointer&lt;ShowAnimation&gt; animation;
 };

 static QMap&lt;QWSWindow*, WindowInfo*&gt; windowMap;

<span class="comment"> /*!
   Constructs the animation for the transition effect used
   when the window associated with \a screen is displayed.
  */</span>
 ShowAnimation::ShowAnimation(QAhiGLScreenPrivate *screen)
     : QTimeLine(animationLength)
 {
     setUpdateInterval(frameSpan);
     connect(this, SIGNAL(valueChanged(qreal)), screen, SLOT(redrawScreen()));
     connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
     start();
 }

 qreal ShowAnimation::valueForTime(int msec)
 {
     const qreal t = msec / qreal(duration());
     return 3*t*t - 2*t*t*t;
 }

 QAhiGLScreenPrivate::QAhiGLScreenPrivate(QAhiGLScreen *s)
     : screen(s), cursor(0), doEffects(false)
 {
     connect(&amp;updateTimer, SIGNAL(timeout()), this, SLOT(redrawScreen()));
 }

<span class="comment"> /*!
   This slot handles the \a event when the \l {QWSServer}
   {window server} emits a window event for the specified
   \a window.

   The \l {QWSServer::WindowEvent} {window events} handled
   are \c Create, \c Destroy, and \c Show. The \c Create
   event creates a new instance of \l {WindowInfo} and stores
   it in a window map to mark the creation of a new window.
   The \c Destroy event causes the \l {WindoInfo} instance
   to be removed from the map and destroyed.

   The \c Show event is the most interesting. If the user
   has started the application with -display ahigl:effects,
   then the \c Show event is handled by creating a small
   \l {ShowAnimation} {animation} for use when the window
   is first shown.
  */</span>
 void QAhiGLScreenPrivate::windowEvent(QWSWindow *window,
                                       QWSServer::WindowEvent event)
 {
     switch (event) {
     case QWSServer::Create:
         windowMap[window] = new WindowInfo;
         break;
     case QWSServer::Show:
         if (doEffects)
             windowMap[window]-&gt;animation = new ShowAnimation(this);
         break;
     case QWSServer::Destroy:
         delete windowMap[window];
         windowMap.remove(window);
         break;
     default:
         break;
     }
 }

<span class="comment"> /*!
   This function assumes the updateTimer is still counting down and stops it
   and then calls redrawScreen() in the public screen driver class QAhiGLScreen.
  */</span>
 void QAhiGLScreenPrivate::redrawScreen()
 {
     updateTimer.stop();
     screen-&gt;redrawScreen();
 }

<span class="comment"> /*!
   \class QAhiGLScreen

   \brief The QAhiGLScreen class is the screen driver for the ATI handheld device interface.

   QAhiGLScreen is implemented with the d-pointer pattern. That is,
   the only data member the class contains is a pointer called d_ptr,
   which means data pointer. It points to an instance of a private
   class called QAhiGLScreenPrivate, where all the screen driver's
   context data members are defined. The d-pointer pattern is used
   so that changes can be made to the screen driver's context data
   members without destroying the binary compatibility of the public
   screen driver class.

   The pure virtual functions found in the base class QScreen are
   listed below. All must have implementations in any screen driver
   class derived from QScreen. All are impemented in this example,
   except for setMode(), which has only been given a stub
   implementation to satisfy the compiler.

   bool connect(const QString &amp; displaySpec);
   void disconnect();
   bool initDevice();
   void setMode(int width, int height, int depth);

   The stub implementation of setMode() is not meant to indicate
   setMode() can be ignored in your own screen driver class. It was
   simply decided not to provide a fully implemented screen driver
   class for the example, which would normally be tailored to your
   device's specific requirements.

   The base class QGLScreen has only one pure virtual function,
   hasOpenGL(), which must return true if your screen driver class
   supports OpenGL.

   QWSWindowSurface * createSurface(const QString &amp; key) const
   QWSWindowSurface * createSurface(QWidget * widget) const
   void exposeRegion(QRegion region, int windowIndex)

  */</span>

<span class="comment"> /*!
   Constructs a new, ATI handheld device screen driver.

   The displayId identifies the Qtopia Core server to connect to.
  */</span>
 QAhiGLScreen::QAhiGLScreen(int displayId) : QGLScreen(displayId)
 {
     d_ptr = new QAhiGLScreenPrivate(this);
     d_ptr-&gt;eglDisplay = EGL_NO_DISPLAY;
     d_ptr-&gt;eglSurface = EGL_NO_SURFACE;
 }

<span class="comment"> /*!
   Destroys this ATI handheld device screen driver.
  */</span>
 QAhiGLScreen::~QAhiGLScreen()
 {
     delete d_ptr;
 }

<span class="comment"> /*!
   \reimp
  */</span>
 bool QAhiGLScreen::connect(const QString &amp;displaySpec)
 {
     <span class="comment">//</span> Hardcoded values for this device
     w = 480;
     h = 640;
     dw = w;
     dh = h;
     d = 16;

     const int dpi = 120;
     physWidth = qRound(dw * 25.4 / dpi);
     physHeight = qRound(dh * 25.4 / dpi);

     if (displaySpec.section(':', 1, 1).contains(&quot;effects&quot;))
         d_ptr-&gt;doEffects = true;

     return true;
 }

<span class="comment"> /*!
   \reimp
  */</span>
 bool QAhiGLScreen::initDevice()
 {
     EGLint version, subversion;
     EGLint attrs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
                        EGL_STENCIL_SIZE, 8, EGL_DEPTH_SIZE, 16,
                        EGL_NONE };
     EGLint numConfig;
     EGLConfig eglConfig;

     d_ptr-&gt;eglDisplay = eglGetDisplay(0);
     if (d_ptr-&gt;eglDisplay == EGL_NO_DISPLAY) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglGetDisplay failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     if (!eglInitialize(d_ptr-&gt;eglDisplay, &amp;version, &amp;subversion)) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglInitialize failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     if (!eglChooseConfig(d_ptr-&gt;eglDisplay, attrs, &amp;eglConfig, 1, &amp;numConfig)) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglChooseConfig failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     static DummyScreen win = { w, h };
     d_ptr-&gt;eglSurface = eglCreateWindowSurface(d_ptr-&gt;eglDisplay, eglConfig,
                                                &amp;win, 0);
     if (d_ptr-&gt;eglSurface == EGL_NO_SURFACE) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglCreateWindowSurface failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     d_ptr-&gt;eglContext = eglCreateContext(d_ptr-&gt;eglDisplay, eglConfig,
                                          EGL_NO_CONTEXT, 0);
     if (d_ptr-&gt;eglContext == EGL_NO_CONTEXT) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglCreateContext failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     if (!eglMakeCurrent(d_ptr-&gt;eglDisplay, d_ptr-&gt;eglSurface, d_ptr-&gt;eglSurface, d_ptr-&gt;eglContext)) {
         qCritical(&quot;QAhiGLScreen::initDevice(): eglMakeCurrent failed: 0x%x&quot;,
                   eglGetError());
         return false;
     }

     d_ptr-&gt;connect(QWSServer::instance(),
                    SIGNAL(windowEvent(QWSWindow*, QWSServer::WindowEvent)),
                    SLOT(windowEvent(QWSWindow*, QWSServer::WindowEvent)));

     d_ptr-&gt;cursor = new QAhiGLCursor;
     qt_screencursor = d_ptr-&gt;cursor;

     return true;
 }

<span class="comment"> /*!
   \reimp
  */</span>
 void QAhiGLScreen::shutdownDevice()
 {
     delete d_ptr-&gt;cursor;
     d_ptr-&gt;cursor = 0;
     qt_screencursor = 0;

     eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE,
                    EGL_NO_SURFACE, EGL_NO_CONTEXT);
     eglDestroyContext(d_ptr-&gt;eglDisplay, d_ptr-&gt;eglContext);
     eglDestroySurface(d_ptr-&gt;eglDisplay, d_ptr-&gt;eglSurface);
     eglTerminate(d_ptr-&gt;eglDisplay);
 }

<span class="comment"> /*!
   \reimp

   In this case, the reimplimentation does nothing. It is
   required because the function is declared as pure virtual
   in the base class QScreen.
  */</span>
 void QAhiGLScreen::disconnect()
 {
 }

<span class="comment"> /*
   This internal function rounds up to the next power of
   two. If v is already a power of two, that same value is
   returned.
  */</span>
 inline static uint nextPowerOfTwo(uint v)
 {
     v--;
     v |= v &gt;&gt; 1;
     v |= v &gt;&gt; 2;
     v |= v &gt;&gt; 4;
     v |= v &gt;&gt; 8;
     v |= v &gt;&gt; 16;
     ++v;
     return v;
 }

<span class="comment"> /*
   This internal function creates a texture from the image img
   and returns its texture identifier.

   The term &quot;texture&quot; is a graphics technology term that refers
   to a pixmap constructed from an image by adding extra points
   of contrast to the otherwise plain color image. The texture
   has a, well, texture, that the original image doesn't have.
  */</span>
 static GLuint createTexture(const QImage &amp;img)
 {
     if (img.isNull())
         return 0;

     int width = img.width();
     int height = img.height();
     int textureWidth;
     int textureHeight;
     GLuint texture;

     glGenTextures(1, &amp;texture);
     textureWidth = nextPowerOfTwo(width);
     textureHeight = nextPowerOfTwo(height);
     glBindTexture(GL_TEXTURE_2D, texture);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

     switch (img.format()) {
     case QImage::Format_RGB16:
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
                      textureWidth,
                      textureHeight, 0,
                      GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);

         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
                         GL_RGB, GL_UNSIGNED_SHORT_5_6_5, img.bits());
         break;

     case QImage::Format_ARGB32_Premultiplied:
     case QImage::Format_ARGB32:
     case QImage::Format_RGB32:
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                      textureWidth,
                      textureHeight, 0,
                      GL_RGBA, GL_UNSIGNED_BYTE, 0);
         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
                         GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
         break;

     default:
         break;
     }

     return texture;
 }

<span class="comment"> /*
   A helper function used by QAhiGLScreen::drawQuad().
  */</span>
 static void drawQuad_helper(GLshort *coords, GLfloat *texCoords)
 {
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
     glEnableClientState(GL_VERTEX_ARRAY);
     glVertexPointer(2, GL_SHORT, 0, coords);
     glEnable(GL_TEXTURE_2D);
     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
     glDisable(GL_TEXTURE_2D);
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 }

<span class="comment"> /*
   A helper function used by QAhiGLScreen::drawQuadWavyFlag().
  */</span>
 static void drawQuad_helper(GLshort *coords, GLfloat *texCoords,
                             int arraySize, int numArrays)
 {
     glEnable(GL_TEXTURE_2D);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
     glEnableClientState(GL_VERTEX_ARRAY);
     glVertexPointer(2, GL_SHORT, 0, coords);

     for (int i = 0; i &lt; numArrays-1; ++i)
         glDrawArrays(GL_TRIANGLE_STRIP, i*arraySize, arraySize);
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     glDisable(GL_TEXTURE_2D);
 }

<span class="comment"> /*
   A convenience function used by QAhiGLScreen::drawQuad().
  */</span>
 static void setRectCoords(GLshort *coords, QRect rect)
 {
     coords[0] = GLshort(rect.left());
     coords[1] = GLshort(rect.top());

     coords[2] = GLshort(rect.right());
     coords[3] = GLshort(rect.top());

     coords[4] = GLshort(rect.right());
     coords[5] = GLshort(rect.bottom());

     coords[6] = GLshort(rect.left());
     coords[7] = GLshort(rect.bottom());
 }

<span class="comment"> /*!
   A helper function used by QAhiGLScreen::drawWindow() and
   QAhiGLScreen::redrawScreen().
  */</span>
 void QAhiGLScreen::drawQuad(const QRect &amp;textureGeometry,
                             const QRect &amp;subGeometry,
                             const QRect &amp;screenGeometry)
 {
     qreal textureWidth = qreal(nextPowerOfTwo(textureGeometry.width()));
     qreal textureHeight = qreal(nextPowerOfTwo(textureGeometry.height()));

     GLshort coords[8];
     setRectCoords(coords, screenGeometry);

     GLfloat texcoords[8];
     texcoords[0] = (subGeometry.left() - textureGeometry.left()) / textureWidth;
     texcoords[1] = (subGeometry.top() - textureGeometry.top()) / textureHeight;

     texcoords[2] = (subGeometry.right() - textureGeometry.left()) / textureWidth;
     texcoords[3] = (subGeometry.top() - textureGeometry.top()) / textureHeight;

     texcoords[4] = (subGeometry.right() - textureGeometry.left()) / textureWidth;
     texcoords[5] = (subGeometry.bottom() - textureGeometry.top()) / textureHeight;

     texcoords[6] = (subGeometry.left() - textureGeometry.left()) / textureWidth;
     texcoords[7] = (subGeometry.bottom() - textureGeometry.top()) / textureHeight;

     drawQuad_helper(coords, texcoords);
 }

<span class="comment"> /*
   My own sine function.
  */</span>
 static qreal mySin(QFixed radians)
 {
     const QFixed twoPI = QFixed::fromReal(2*M_PI);

     const int tableSize = 40;
     static int *table = 0;

     if (!table) {
         table = new int[tableSize];
         for (int i = 0; i &lt; tableSize; ++i) {
             table[i] = qRound(sin(M_PI*(i*360.0/40.0)/180.0) * 16776960.0);
         }
     }

     QFixed tableLookup = radians*tableSize/twoPI;
     return table[tableLookup.truncate()%tableSize]/16776960.0;
 }

<span class="comment"> /*
   My own cosine function.
  */</span>
 static qreal myCos(QFixed radians)
 {
     const int twoPI = qRound(2*M_PI);

     const int tableSize = 40;
     static int *table = 0;

     if (!table) {
         table = new int[tableSize];
         for (int i = 0; i &lt; tableSize; ++i) {
             table[i] = int(cos(M_PI*(i*360.0/40.0)/180.0) * 16776960.0);
         }
     }

     QFixed tableLookup = radians*tableSize/twoPI;
     return table[tableLookup.truncate()%tableSize]/16776960.0;
 }

<span class="comment"> //</span> number of grid cells in wavy flag tesselation in x- and y-direction
 const int subdivisions = 10;

<span class="comment"> /*
   A helper function used by drawQuadWavyFlag(). It computes
   coordinates for grid cells for a wavy flag tesselation and
   stores them in the array called coords.
  */</span>
 static void setFlagCoords(GLshort *coords,
                           QRectF screenGeometry,
                           int frameNum,
                           qreal progress)
 {
     int coordIndex = 0;
     qreal waveHeight = 30.0*(1.0-progress);
     for (int j = 0; j &lt; subdivisions-1; ++j) {
         for (int i = 0; i &lt; subdivisions; ++i) {
             qreal c;
             c = screenGeometry.left()
                 + (i * screenGeometry.width() / (subdivisions - 1))
                 + waveHeight * qRound(mySin(QFixed::fromReal(M_PI * 20 * (frameNum + i) / 180.0)))
                 + waveHeight * qRound(myCos(QFixed::fromReal(M_PI * 20 * (frameNum + j) / 180.0)));
             coords[coordIndex++] = qRound(c);
             c = screenGeometry.top()
                 + (j * screenGeometry.height() / (subdivisions - 1))
                 + waveHeight * mySin(QFixed::fromReal(M_PI * 20 * (frameNum + i) / 180.0))
                 + waveHeight * myCos(QFixed::fromReal(M_PI * 20 * (frameNum + j) / 180.0));
             coords[coordIndex++] = qRound(c);
             c = screenGeometry.left() + (i * screenGeometry.width() / (subdivisions - 1))
                 + waveHeight * mySin(QFixed::fromReal(M_PI * 20 * (frameNum + i) / 180.0))
                 + waveHeight * myCos(QFixed::fromReal(M_PI * 20 * (frameNum + (j+1)) / 180.0));
             coords[coordIndex++] = qRound(c);

             c = screenGeometry.top()
                 + ((j + 1) * screenGeometry.height() / (subdivisions - 1))
                 + waveHeight * mySin(QFixed::fromReal(M_PI * 20 * (frameNum + i) / 180.0))
                 + waveHeight * myCos(QFixed::fromReal(M_PI * 20 * (frameNum + (j + 1)) / 180.0));
             coords[coordIndex++] = qRound(c);
         }
     }
 }

 static void setFlagTexCoords(GLfloat *texcoords,
                              const QRectF &amp;subTexGeometry,
                              const QRectF &amp;textureGeometry,
                              int textureWidth, int textureHeight)
 {
     qreal topLeftX = (subTexGeometry.left() - textureGeometry.left())/textureWidth;
     qreal topLeftY = (textureGeometry.height() - (subTexGeometry.top() - textureGeometry.top()))/textureHeight;

     qreal width = (subTexGeometry.right() - textureGeometry.left())/textureWidth - topLeftX;
     qreal height = (textureGeometry.height() - (subTexGeometry.bottom() - textureGeometry.top()))/textureHeight - topLeftY;

     int coordIndex = 0;
     qreal spacing = subdivisions - 1;
     for (int j = 0; j &lt; subdivisions-1; ++j) {
         for (int i = 0; i &lt; subdivisions; ++i) {
             texcoords[coordIndex++] = topLeftX + (i*width) / spacing;
             texcoords[coordIndex++] = topLeftY + (j*height) / spacing;
             texcoords[coordIndex++] = topLeftX + (i*width) / spacing;
             texcoords[coordIndex++] = topLeftY + ((j+1)*height) / spacing;
         }
     }
 }

 void QAhiGLScreen::drawQuadWavyFlag(const QRect &amp;textureGeometry,
                                     const QRect &amp;subTexGeometry,
                                     const QRect &amp;screenGeometry,
                                     qreal progress)
 {
     const int textureWidth = nextPowerOfTwo(textureGeometry.width());
     const int textureHeight = nextPowerOfTwo(textureGeometry.height());

     static int frameNum = 0;

     GLshort coords[subdivisions*subdivisions*2*2];
     setFlagCoords(coords, screenGeometry, frameNum++, progress);

     GLfloat texcoords[subdivisions*subdivisions*2*2];
     setFlagTexCoords(texcoords, subTexGeometry, textureGeometry,
                      textureWidth, textureHeight);

     drawQuad_helper(coords, texcoords, subdivisions*2, subdivisions);
 }

 void QAhiGLScreen::invalidateTexture(int windowIndex)
 {
     if (windowIndex &lt; 0)
         return;

     QList&lt;QWSWindow*&gt; windows = QWSServer::instance()-&gt;clientWindows();
     if (windowIndex &gt; windows.size() - 1)
         return;

     QWSWindow *win = windows.at(windowIndex);
     if (!win)
         return;

     WindowInfo *info = windowMap[win];
     if (info-&gt;texture) {
         glDeleteTextures(1, &amp;info-&gt;texture);
         info-&gt;texture = 0;
     }
 }

 void QAhiGLScreen::drawWindow(QWSWindow *win, qreal progress)
 {
     const QRect screenRect = win-&gt;allocatedRegion().boundingRect();
     QRect drawRect = screenRect;

     glColor4f(1.0, 1.0, 1.0, progress);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glEnable(GL_BLEND);

     QWSWindowSurface *surface = win-&gt;windowSurface();
     if (!surface)
         return;

     if (progress &gt;= 1.0) {
         if (surface-&gt;key() == QLatin1String(&quot;ahigl&quot;)) {
             drawRect.setCoords(drawRect.left(), drawRect.bottom(),
                                drawRect.right(), drawRect.top());
         }
         drawQuad(win-&gt;requestedRegion().boundingRect(), screenRect, drawRect);
         return;
     }

     const int dx = qRound((1 - progress) * drawRect.width() / 2);
     const int dy = qRound((1 - progress) * drawRect.height() / 2);

     drawRect.adjust(dx, dy, -dx, -dy);

     if (surface-&gt;key() != QLatin1String(&quot;ahigl&quot;)) {
         drawRect.setCoords(drawRect.left(), drawRect.bottom(),
                            drawRect.right(), drawRect.top());
     }

     drawQuadWavyFlag(win-&gt;requestedRegion().boundingRect(), screenRect,
                      drawRect, progress);
 }

<span class="comment"> /*!
   The window compositions are constructed here.
  */</span>
 void QAhiGLScreen::redrawScreen()
 {
     glBindFramebufferOES(GL_FRAMEBUFFER_EXT, 0);
     glMatrixMode(GL_PROJECTION);
     glPushMatrix();
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();

     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrthof(0, w, h, 0, -999999, 999999);
     glViewport(0, 0, w, h);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();

     <span class="comment">//</span> Fill background color

     QColor bgColor = QWSServer::instance()-&gt;backgroundBrush().color();
     glClearColor(bgColor.redF(), bgColor.greenF(),
                  bgColor.blueF(), bgColor.alphaF());
     glClear(GL_COLOR_BUFFER_BIT);

     <span class="comment">//</span> Draw all windows

     glDisable(GL_CULL_FACE);
     glDisable(GL_DEPTH_TEST);
     glDisable(GL_STENCIL_TEST);
     glEnable(GL_BLEND);
     glBlendFunc(GL_ONE, GL_ZERO);
     glDisable(GL_ALPHA_TEST);
     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

     QList&lt;QWSWindow*&gt; windows = QWSServer::instance()-&gt;clientWindows();
     for (int i = windows.size() - 1; i &gt;= 0; --i) {
         QWSWindow *win = windows.at(i);
         QWSWindowSurface *surface = win-&gt;windowSurface();
         if (!surface)
             continue;

         WindowInfo *info = windowMap[win];

         if (!info-&gt;texture) {
             if (surface-&gt;key() == QLatin1String(&quot;ahigl&quot;))
                 info-&gt;texture = static_cast&lt;QAhiGLWindowSurface*&gt;(surface)-&gt;textureId();
             else
                 info-&gt;texture = createTexture(surface-&gt;image());
         }
         qreal progress;
         if (info-&gt;animation)
             progress = info-&gt;animation-&gt;currentValue();
         else
             progress = 1.0;

         glBindTexture(GL_TEXTURE_2D, info-&gt;texture);
         drawWindow(win, progress);
     } <span class="comment">//</span> for i

     <span class="comment">//</span> Draw cursor

     const QAhiGLCursor *cursor = d_ptr-&gt;cursor;
     if (cursor-&gt;texture) {
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glBindTexture(GL_TEXTURE_2D, d_ptr-&gt;cursor-&gt;texture);
         drawQuad(cursor-&gt;boundingRect(), cursor-&gt;boundingRect(),
                  cursor-&gt;boundingRect());
     }

     glPopMatrix();
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
     glMatrixMode(GL_MODELVIEW);

     eglSwapBuffers(d_ptr-&gt;eglDisplay, d_ptr-&gt;eglSurface);
 }

<span class="comment"> /*!
   \reimp

  */</span>
 void QAhiGLScreen::exposeRegion(QRegion r, int windowIndex)
 {
     if ((r &amp; region()).isEmpty())
         return;

     invalidateTexture(windowIndex);

     if (!d_ptr-&gt;updateTimer.isActive())
         d_ptr-&gt;updateTimer.start(frameSpan);
 }

<span class="comment"> /*!
   \reimp

   This overloading of createSurface() is called on the client side to
   create a window surface for a new window. If the \a widget is a
   QGLWidget, or if the widget's width and height are both less than or
   equal to 256, it creates an instance of QAhiGLWindowSurface.
   Otherwise, it calls QScreen::createSurface() to create a non-OpenGL
   window surface. The pointer to the new window surface is returned.

   Note that this function first asks whether this application is the
   server, and it only creates an instance of QAhiGLWindowSurface if
   the answer is yes. What this means is we only let the server have
   access to the OpenGL hardware, because of an implementation
   restyriction in the OpenGL libraries we are using. Thus only clients
   that are in the server process get to create OpenGL window surfaces.
  */</span>
 QWSWindowSurface* QAhiGLScreen::createSurface(QWidget *widget) const
 {
     if (QApplication::type() == QApplication::GuiServer) {
         if (qobject_cast&lt;QGLWidget*&gt;(widget)) {
             return new QAhiGLWindowSurface(widget,
                                            d_ptr-&gt;eglDisplay,
                                            d_ptr-&gt;eglSurface,
                                            d_ptr-&gt;eglContext);
         }

         const QRect rect = widget-&gt;frameGeometry();
         if (rect.width() &lt;= 256 &amp;&amp; rect.height() &lt;= 256) {
             return new QAhiGLWindowSurface(widget,
                                            d_ptr-&gt;eglDisplay,
                                            d_ptr-&gt;eglSurface,
                                            d_ptr-&gt;eglContext);
         }
     }

     return QScreen::createSurface(widget);
 }

<span class="comment"> /*!
   \reimp

   This overloading of createSurface() is called on the server side
   to manage a window surface corresponding to a window surface
   already created on the client side.

   If the \a key is &quot;ahigl&quot;, create an instance of QAhiGLWindowSurface
   and return it. Otherwise, call QScreen::createSurface() and return
   the window surface it creates.

   See QScreen::createSurface().
  */</span>
 QWSWindowSurface* QAhiGLScreen::createSurface(const QString &amp;key) const
 {
     if (key == QLatin1String(&quot;ahigl&quot;)) {
         return new QAhiGLWindowSurface(d_ptr-&gt;eglDisplay,
                                        d_ptr-&gt;eglSurface,
                                        d_ptr-&gt;eglContext);
     }

     return QScreen::createSurface(key);
 }

<span class="comment"> /*!
   This function would normally reset the frame buffer resolution
   according to \a width, \a height, and the bit \a depth. It would
   then notify other applications that their frame buffer size had
   changed so they could redraw. The function is a no-op in this
   example, which means the example screen driver can't change its
   frame buffer resolution. There is no significance to that in the
   example. You would normally implement setMode() in an OpenGL
   screen driver. This no-op reimplementation is required here
   because setMode() in QScreen is a pure virtual function.

   See QScreen::setMode()
  */</span>
 void QAhiGLScreen::setMode(int width, int height, int depth)
 {
     Q_UNUSED(width);
     Q_UNUSED(height);
     Q_UNUSED(depth);
 }

<span class="comment"> /*!
   This function would normally be reimplemented to prevent the
   screen driver from updating the screen if \a on is true. It is a
   no-op in this example, which means the screen driver can always
   update the screen.

   See QScreen::blank().
  */</span>
 void QAhiGLScreen::blank(bool on)
 {
     Q_UNUSED(on);
 }

<span class="comment"> /*!
   This function always returns true, since the purpose of
   this screen driver class is to implement an OpenGL ES
   screen driver. In some other class designed to handle both
   OpenGL and non-OpenGL graphics, it might test the system to
   determine whether OpenGL graphics are supported and return
   true or false accordingly.
  */</span>
 bool QAhiGLScreen::hasOpenGL()
 {
     return true;
 }

 #include &quot;qscreenahigl_qws.moc&quot;</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
